/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.swt.graphics.Color;

import edu.pku.sei.modeler.uml.ResourceFactory;
import edu.pku.sei.modeler.uml.test.FragmentBorder;

/**
 * @generated
 * 
 */
public class InteractionFigure extends CommonFigure {
	//private static final Color FILL_COLOR = new Color(Display.getDefault(),237,232,135);
	private static final Color FILL_COLOR = ColorConstants.tooltipBackground;
	
	private TextFlow text;

	private FlowPage titleFlow;

	public InteractionFigure(String tag) {

		setBorder(new edu.pku.sei.modeler.uml.test.FragmentBorder());
		setLayoutManager(new XYLayout());
		setBackgroundColor(ColorConstants.listBackground);
		setOpaque(true);

		text = new TextFlow();
		titleFlow = new FlowPage();
		titleFlow.add(new TextFlow("  "));
		titleFlow.add(new TextFlow(tag));
		titleFlow.add(new TextFlow("  "));
		titleFlow.add(text);
		add(titleFlow);

	}
	
	public void changeLineColor(Color color) {
		this.setForegroundColor(color);
		((FragmentBorder)this.getBorder()).setColor(color);
	}
	
	public InteractionFigure() {
		this("sd");
	}

//	public Rectangle getPreferredBounds(Rectangle suggestedBounds) {
//		if (suggestedBounds.width == 0)
//			return new Rectangle(suggestedBounds.getLocation(), DEFAULT_SIZE);
//		else
//			return suggestedBounds;
//	}

	public Dimension getPreferredSize(int w, int h) {
		Dimension prefSize = super.getPreferredSize(w, h);
		Dimension defaultSize = new Dimension(200, 100);
		prefSize.union(defaultSize);
		return prefSize;
	}

	protected void paintFigure(Graphics graphics) {
		titleFlow.setFont(ResourceFactory.getHeadFont());
		titleFlow.setForegroundColor(ColorConstants.black);
		Rectangle rect = getBounds().getCopy();
		rect.crop(new Insets(2, 0, 2, 0));
		graphics.setBackgroundColor(FILL_COLOR);
//		graphics.setBackgroundColor(ColorConstants.white);
		graphics.setForegroundColor(this.getForegroundColor());
		graphics.fillRectangle(rect);
		((FragmentBorder) getBorder()).setBackgroundColor(this.getBackgroundColor());
		((FragmentBorder) getBorder()).setColor(this.getForegroundColor());
		((FragmentBorder) getBorder()).paintLabelBackground(this, graphics);
	}

	public TextFlow getText() {
		return text;
	}

	public void setText(TextFlow text) {
		this.text = text;
	}

	@Override
	protected boolean useLocalCoordinates() {
		return true; // TODO Investigate if returning false avoids
		// absolute/local coversions
	}

	public void validate() {
		if (isValid())
			return;
		this.invalidateTree();
		super.validate();

		Dimension prefSize = titleFlow.getPreferredSize();
		if (!titleFlow.getSize().equals(prefSize)) {
			titleFlow.setSize(prefSize);
		}
		// updateAnchors();
	}

	// public void setBounds(Rectangle rect) {
	// super.setBounds(rect);
	// if (root != null)
	// root.setSize(rect.width, rect.height);
	// }

	// protected void paintShadow(Graphics g) {
	// // TODO: draw the figure's own shadow here
	// }

	public Rectangle getLocalTextBounds() {
		return text.getBounds().getCopy();
	}
}
